package com.example.taobaounion3.presenter.impl;

import android.webkit.URLUtil;

import com.example.taobaounion3.model.Api;
import com.example.taobaounion3.model.domain.SelectedContent;
import com.example.taobaounion3.model.domain.SelectedPageCategory;
import com.example.taobaounion3.presenter.ISelectedPagePresenter;
import com.example.taobaounion3.utils.LogUtils;
import com.example.taobaounion3.utils.RetrofitManager;
import com.example.taobaounion3.utils.urlUtils;
import com.example.taobaounion3.view.ISelectedPageCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class SelectedPagePresenterlmpl implements ISelectedPagePresenter {
    private final Api mApi;
    private ISelectedPageCallback mViewCallback=null;

    public SelectedPagePresenterlmpl(){
        //抽取拿retrofit的过程
        Retrofit retrofit=RetrofitManager.getOurInstance().getRetrofit();
        mApi=retrofit.create(Api.class);
    }

    @Override
    public void getSelectedCategories() {
        if (mViewCallback!=null) {
            mViewCallback.onLoading();
        }
        Call<SelectedPageCategory> task=mApi.getSelectedPageCategories();
        task.enqueue(new Callback<SelectedPageCategory>() {
            @Override
            public void onResponse(Call<SelectedPageCategory> call, Response<SelectedPageCategory> response) {
                int resultCode =response.code();
                LogUtils.d(SelectedPagePresenterlmpl.this,"Seclet code-->"+resultCode);
                if (resultCode== HttpURLConnection.HTTP_OK) {
                    SelectedPageCategory result=response.body();
                    //通知UI更新
                    if (mViewCallback!=null) {
                        mViewCallback.onCategoriesLoaded(result);
                    }
                }else {
                    onLoadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedPageCategory> call, Throwable t) {
                onLoadedError();
            }
        });
    }

    private void onLoadedError() {
        if (mViewCallback!=null) {
            mViewCallback.onError();
        }
    }

    @Override
    public void getSelectedContentByCategory(SelectedPageCategory.DataBean item) {
        int categoryId = item.getFavorites_id();
        LogUtils.d(this, "categoryId-->" + categoryId);
        String targetUrl = urlUtils.getSelectedPageContentUrl(categoryId);
        Call<SelectedContent> task = mApi.getSelectedPageContent(targetUrl);
        task.enqueue(new Callback<SelectedContent>() {
            @Override
            public void onResponse(Call<SelectedContent> call, Response<SelectedContent> response) {
                int resultCode = response.code();
                LogUtils.d(SelectedPagePresenterlmpl.this, "Content code-->" + resultCode);
                if (resultCode == HttpURLConnection.HTTP_OK) {
                    SelectedContent result = response.body();
                    if (mViewCallback != null) {
                        mViewCallback.onContentLoaded(result);
                    }
                } else {
                    onLoadedError();
                }
            }

            @Override
            public void onFailure(Call<SelectedContent> call, Throwable t) {
                onLoadedError();
            }
        });

    }

    @Override
    public void reloadContent() {
        this.getSelectedCategories();
    }

    @Override
    public void registerViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback=callback;
    }

    @Override
    public void unregisterViewCallback(ISelectedPageCallback callback) {
        this.mViewCallback=null;
    }
}
